﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using WCFAuthorization.Service.Crypt;

namespace WCFAuthorization.Service
{
    public class Cacher
    {
        static readonly string DATA_FILE_NAME = "Users.dat";
        static readonly string CRYPT_BASE64_KEY = "uOLFLnlJzOB2oqhB1BfJCiVfgH6lDhDYo9PYW3YmotE=";
        static UserCollection _users = new UserCollection();

        public static UserCollection UsersCache
        {
            get
            {
                return _users;
            }
        }

        static IList<string> _blacks = new List<string>();
        public static IList<string> BlackList
        {
            get
            {
                return _blacks;
            }
        }

        public static void LoadBlackList()
        {
            string blackfile = Path.Combine(Helper.AppPath(), "config.bin");
            if (File.Exists(blackfile))
            {
                using (StreamReader reader = new StreamReader(blackfile, Encoding.Default))
                {
                    string srLine = reader.ReadLine();
                    while (srLine != null)
                    {
                        if (!string.IsNullOrEmpty(srLine))
                        { _blacks.Add(srLine.Trim()); }
                        srLine = reader.ReadLine();
                    }
                }
            }
        }

        public static void LoadUsers()
        {
            string fileName = Path.Combine(Helper.AppPath(), Cacher.DATA_FILE_NAME);
            if (File.Exists(fileName))
            {
                Rijndael rj = Rijndael.Create();
                try
                {
                    using (SymmetricCryptographer crypt = new SymmetricCryptographer(rj.GetType()))
                    using (FileStream reader = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                    {
                        byte[] buff = new byte[reader.Length]; reader.Read(buff, 0, buff.Length);
                        byte[] key = Convert.FromBase64String(Cacher.CRYPT_BASE64_KEY);
                        byte[] decode = crypt.Decrypt(buff, key);
                        _users = Helper.XmlDeserialize<UserCollection>(Encoding.Default.GetString(decode));
                    }
                }
                catch { }
                finally
                {
                    rj.Dispose();
                }
            }
        }

        public static void SaveUsers()
        {
            string fileName = Path.Combine(Helper.AppPath(), Cacher.DATA_FILE_NAME);
            Rijndael rj = Rijndael.Create();
            try
            {
                string xml = Helper.XmlSerialize<UserCollection>(_users);
                using (SymmetricCryptographer crypt = new SymmetricCryptographer(rj.GetType()))
                using (FileStream writer = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                {
                    byte[] key = Convert.FromBase64String(Cacher.CRYPT_BASE64_KEY);
                    byte[] buff = crypt.Encrypt(Encoding.Default.GetBytes(xml), key);
                    writer.Write(buff, 0, buff.Length);
                }
            }
            catch { }
            finally
            {
                rj.Dispose();
            }
        }
    }
}